//获取应用实例
var app = getApp()
const _request = require("../../../utils/request.js"); // 引入require;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    workday: "",
    servicealls: [],
    services: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 获取用户个人信息
    _request.get('/storeCenterInfo/getStoreCenterInfo', {
      id: wx.getStorageSync('userid')
    }).then((res) => {
      var services = res.data.data.services.map((v) => v['id']);
      _request.postObj('/category/getAll', {}).then((res) => {
        var servicealls = res.data.data
        for (let i = 0; i < servicealls.length; i++) {
          if (services.indexOf(servicealls[i]['id']) != -1) {
            servicealls[i]['checked'] = true;
          }
        }
        this.setData({
          servicealls: servicealls
        })
      }).catch((error) => {
        console.log(error, 'error');
      })
    }).catch((error) => {
      console.log(error, 'error');
    })
    wx.enableAlertBeforeUnload({
      message: '内容未保存, 点击返回将导致未保存内容清空, 请确认是否返回?'
    })
  },
  onWorkTap: function (e) {
    let id = e.currentTarget.dataset.item;
    let servicealls = this.data.servicealls;
    for (let j = 0; j < servicealls.length; j++) {
      if (id == servicealls[j]['id']) {
        servicealls[j]['checked'] = servicealls[j]['checked'] ? false : true;
      }
    }
    this.setData({
      servicealls: servicealls
    })
  },
  onAdd: function (e) {
    let services = [];
    let servicealls = this.data.servicealls;
    for (let j = 0; j < servicealls.length; j++) {
      if (servicealls[j]['checked']) {
        services.push(servicealls[j]['id']);
      }
    }
    if (services.length == 0) {
      wx.showToast({
        title: '请至少选择一种服务类型',
        icon: 'none',
        duration: 2000,
      })
      return;
    }
    _request.postObj('/storeCenterInfo/updateServices', {
      storeId: wx.getStorageSync('storeId'),
      services: services,
    }).then((res) => {
      if (res.data.code === 200) {
        wx.disableAlertBeforeUnload();
        wx.showToast({
          title: '修改成功',
          icon: 'success',
          duration: 2000
        })
        wx.navigateBack({
          delta: 0
        })
      } else {
        wx.showToast({
          title: res.data.message,
          icon: 'none',
          duration: 2000,
        })
      }

    }).catch((error) => {
      console.log(error, 'error');
    })
  }
})